<script lang="ts" setup name="MetaIndex">
  import appStore from '@/store'
  import metaHeader from '@/components/Header.vue'
  import leftMenu from '@/components/LeftMenu.vue'
  import { ElConfigProvider, ElMessageBox } from 'element-plus'
  import zhCn from 'element-plus/lib/locale/lang/zh-cn'
  import { useRoute, useRouter } from 'vue-router'
  import VueCookies from 'vue-cookies'
  const cookie: any = VueCookies
  const router = useRouter()
  const route = useRoute()
  let username = cookie.get('username')
  let nowTime = Math.round(new Date().getTime() / 1000)
  let ExpireTime = cookie.get('ExpireTime') * 1
  let timeout = ExpireTime - nowTime
  const go = async () => {
    await router.isReady()
    if (route.name && route.name != 'Login') {
      router.push({ name: route.name })
    } else {
      router.push({ name: 'Homepage' })
    }
  }
  if (timeout > 0) {
    appStore.userStore.updateLogin(true)
    appStore.userStore.updateName(username)
    go()
  } else {
    if (cookie.get('ExpireTime')) {
      ElMessageBox.alert('登录已过期，请重新登录！', {
        confirmButtonText: '确认'
      })
      cookie.remove('ExpireTime')
    }
    appStore.userStore.$reset()
    localStorage.clear()
    router.push({ name: 'Login' })
  }
</script>
<template>
  <div id="main">
    <div class="Login">
      <metaHeader v-if="appStore.userStore.isLogin"></metaHeader>
      <div :class="appStore.userStore.isLogin ? 'outbox' : ''">
        <leftMenu v-if="appStore.userStore.isLogin"></leftMenu>
        <div class="router-css" :style="!appStore.userStore.isLogin ? 'padding:0;height:100%' : ''">
          <el-config-provider :locale="zhCn">
            <router-view v-if="!appStore.userStore.changeId" />
          </el-config-provider>
        </div>
      </div>
    </div>
  </div>
</template>
<style lang="scss" scoped>
  .outbox {
    display: flex;
    background: $bac-color;
    height: 100%;
    width: 100%;
    .router-css {
      max-height: calc(100vh - 160px);
      width: 100%;
      overflow-y: auto;
      padding: 20px 20px 90px 20px;
    }
  }
</style>
